﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.Models/BoxStateModel
* 类       名: BoxStateModel
* 功      能:  TD
* 时      间:  2024-05-21 10:31:09
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace WaferSelectV3.Models
{
    public enum BoxesStates
    {
        Full, // 满料
        Using, // 正在使用
        Free, // 空闲状态
        Locked // 锁定状态
    }

    public class BoxStateModel : BindableBase
    {
        private int _id;

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }


        private int _boxNumber;

        public int BoxNumber
        {
            get { return _boxNumber; }
            set
            {
                _boxNumber = value;
                RaisePropertyChanged();
            }
        }

        public int UpBoxState = (int)BoxesStates.Free;

        public int DownBoxState = (int)BoxesStates.Free;

        /// <summary>
        /// 锁框时间
        /// </summary>
        public double LockedSeconds;


        private string? _levelType;
        /// <summary>
        /// 料盒保存的等级类型,可以有多个值,一个料盒可能保存多个类型,一个类型可能保存多个料盒
        /// </summary>
        public string? LevelType
        {
            get { return _levelType; }
            set
            {
                _levelType = value;
                RaisePropertyChanged();
            }
        }


        private int _upCountPc;

        /// <summary>
        /// PC统计的个数,一般PC统计的个数比PLC的要超前几个
        /// </summary>
        public int UpCountPc
        {
            get { return _upCountPc; }
            set
            {
                _upCountPc = value;
                if (value != UpCountPc && value < CountMax && value != 0)
                {
                    UpBoxState = (int)BoxesStates.Using; // 如果一直在变化,就认为这个框一直在使用
                }
                else if (value != UpCountPc && value >= CountMax)
                {
                    UpBoxState = (int)BoxesStates.Full; // 满料状态,这里要做锁框的动作,什么时候解锁呢
                }
                else if (value != UpCountPc && value == 0)
                {
                    UpBoxState = (int)BoxesStates.Free; // 如果为0,就认为是Free状态
                }
            }
        }

        private int _downCountPc;
        /// <summary>
        /// PC统计的下框的数量,PC统计的值主要用来改变框的是否可用状态
        /// </summary>
        public int DownCountPc
        {
            get { return _downCountPc; }
            set
            {
                _downCountPc = value;
                if (value != DownCountPc && value < CountMax && value != 0)
                {
                    DownBoxState = (int)BoxesStates.Using;
                }
                else if (value != DownCountPc && value > CountMax)
                {
                    DownBoxState = (int)BoxesStates.Full;
                }
                else if (value != DownCountPc && value == 0 && DownBoxState != (int)BoxesStates.Locked)
                {
                    DownBoxState = (int)BoxesStates.Free;
                }
            }
        }


        private int _upCountPlc;
        /// <summary>
        /// 从PLC那边读取的料盒状态
        /// </summary>
        public int UpCountPlc
        {
            get { return _upCountPlc; }
            set
            {
                if (value != UpCountPc)
                {
                    if (CountColorIndex == 0)
                    {
                        UpCountColor = ColorList[CountColorIndex];
                        CountColorIndex = 1;
                    }
                    else
                    {
                        UpCountColor = ColorList[CountColorIndex];
                        CountColorIndex = 0;
                    }
                }
                else
                {
                    UpCountColor = ColorList[0];
                }
                _upCountPlc = value;
                RaisePropertyChanged();

                if (UpCountPc < UpCountPlc)
                {
                    UpCountPc = UpCountPlc;
                }
                if (UpCountPlc >= CountMax)
                {
                    UpBoxState = (int)BoxesStates.Full;
                    UpCountColor = ColorList[2];
                }
                else if (UpCountPlc == 0)
                {
                    UpBoxState = (int)BoxesStates.Free;
                    UpCountPc = 0;
                    UpCountColor = ColorList[0];
                }
            }
        }

        private int _downCountPlc;
        /// <summary>
        /// 从PLC那边读取的料盒状态
        /// </summary>
        public int DownCountPlc
        {
            get { return _downCountPlc; }
            set
            {
                if (value != DownCountPlc)
                {
                    if (CountColorIndex == 0)
                    {
                        DownCountColor = ColorList[CountColorIndex];
                        CountColorIndex = 1;
                    }
                    else
                    {
                        DownCountColor = ColorList[CountColorIndex];
                        CountColorIndex = 0;
                    }
                }
                else
                {
                    DownCountColor = ColorList[0];
                }
                _downCountPlc = value;
                RaisePropertyChanged();

                if (DownCountPc < DownCountPlc)
                {
                    DownCountPc = DownCountPlc;
                }
                if (DownCountPlc >= CountMax)
                {
                    DownBoxState = (int)BoxesStates.Full;
                    DownCountColor = ColorList[2];
                }
                else if (DownCountPlc == 0)
                {
                    DownBoxState = (int)BoxesStates.Free;
                    DownCountPc = 0;
                    DownCountColor = ColorList[0];
                }
            }
        }

        public int CountColorIndex = 0;
        public SolidColorBrush[] ColorList = new SolidColorBrush[]
        {
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#EEEEEE")),
            new SolidColorBrush(Colors.SpringGreen),
            new SolidColorBrush(Colors.Red), // 红色
        };

        private SolidColorBrush? _upCountColor;

        public SolidColorBrush? UpCountColor
        {
            get { return _upCountColor; }
            set
            {
                _upCountColor = value;
                RaisePropertyChanged();
            }
        }

        private SolidColorBrush? _downCountColor;

        public SolidColorBrush? DownCountColor
        {
            get { return _downCountColor; }
            set
            {
                _downCountColor = value;
                RaisePropertyChanged();
            }
        }

        private int _countMax;
        /// <summary>
        /// 存放的最大数量
        /// </summary>
        public int CountMax
        {
            get { return _countMax; }
            set
            {
                _countMax = value;
                RaisePropertyChanged();
            }
        }


    }
}
